package sexica.foreplay;

import java.util.Collections;
import java.util.Random;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Sexica4PlayActivity extends Activity 

{
		//Creates a random generator from java utils.. 
		Random generator = new Random();
        ArrayList mList;
        ArrayList wList;
		
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   

        

        {
        final Button buttonPlay = (Button) findViewById(R.id.buttonPlay);
        buttonPlay.setOnClickListener(new View.OnClickListener() 
        	{
            	public void onClick(View v) 
            	{	
            	gameCode();
            	}
        });
    }
}
    

    
int i = 0;
TextView w;
TextView m;


@SuppressWarnings("unchecked")
public void gameCode()
    {
       
		//Loads game layout
        setContentView(R.layout.game);
       
        //If, else if statements that loads in the Array to the arraylist
	if (mList == null) 
	{
			//Creates the String Array mQuestions and loads the array from the xml file strings.xml under values
	     String[] mQuestions = getResources().getStringArray(R.array.mQuestions);
	     //Creates a new ArrayList with the name mList
	     mList = new ArrayList();
	     //Loads in the objects/data from mQuestions to the arraylist mList
	     Collections.addAll(mList, mQuestions);
	} 
	
	//This is supposed to run when you run out of objects in the array (questions)
	else if (mList.isEmpty()) 
	{
		//ATM when you run out of objects, the program crashes. I reckon is it because we do not check if the arraylist is empty with a value of 0, null or -1
		m = (TextView)findViewById(R.id.textView1);
    	m.setText("You've run out of questions!"); 
    	final Button buttonNext = (Button) findViewById(R.id.buttonNext);
    	buttonNext.setText("Click here to return to start screen");
    	buttonNext.setOnClickListener(new View.OnClickListener() 
    		{
				@Override
				public void onClick(View v) 
				{
					System.exit(0);
					
				}
    		});
        
	}
	//Look at the comments at mList
	if (wList == null) 
	{
	   String[] wQuestions = getResources().getStringArray(R.array.wQuestions);
	   wList = new ArrayList();
	   Collections.addAll(wList, wQuestions);
	} 
	else if (wList.isEmpty()) 
	{
		w = (TextView)findViewById(R.id.textView1);
    	w.setText("You've run out of questions!");
    	final Button buttonNext = (Button) findViewById(R.id.buttonNext);
    	buttonNext.setText("Click here to exit the game");
    	buttonNext.setOnClickListener(new View.OnClickListener() 
    		{
				@Override
				public void onClick(View v) 
				{
					System.exit(0);
				
				}
    		});
	}
    	if (mList.size() > 0 && wList.size() > 0)
    		{
    			//Takes a random ID from the arraylist
    			int rndm = generator.nextInt(mList.size());
    			int rndw = generator.nextInt(wList.size());
    	
    			//Sets the Strings to a random number from one of the array lists
    			String qMan = (String) mList.get(rndm);
    			String qWoman = (String) wList.get(rndw);
    			if(i == 0)
    				{
    					//Defines that w is textView1 with the value of qWoman
    					w = (TextView)findViewById(R.id.textView1);
    					w.setText(qWoman);
    					//Removes the random number from the ArrayList
    					wList.remove(rndw);
    					i = 1;
    					//The next button loads it all over again
    					final Button buttonNext = (Button) findViewById(R.id.buttonNext);
    					buttonNext.setOnClickListener(new View.OnClickListener() 
    						{
    							public void onClick(View v) 
    							{
    								gameCode();
    							}
    						});
    					}
    				else
    					{
    						m = (TextView)findViewById(R.id.textView1);
    						m.setText(qMan);
    						mList.remove(rndm);
    						i = 0;
    						final Button buttonNext = (Button) findViewById(R.id.buttonNext);
    						buttonNext.setOnClickListener(new View.OnClickListener() 
    						{
    							public void onClick(View v) 
    							{
    								gameCode();
    							}
    						});
    					}
    				}
     
    		}  

}

